package com.hengtongxing.hejiayun.shop;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.gyf.immersionbar.ImmersionBar;
import com.hengtongxing.hejiayun.R;
import com.hengtongxing.hejiayun.base.BaseActivity;
import com.hengtongxing.hejiayun.base.DataCallBack;
import com.hengtongxing.hejiayun.bean.CommentListBean;
import com.hengtongxing.hejiayun.bean.ShopCartListBean;
import com.hengtongxing.hejiayun.bean.ShopDetailsBean;
import com.hengtongxing.hejiayun.dialog.ShopCartDialog;
import com.hengtongxing.hejiayun.homepage.model.HomePageModelImpl;
import com.hengtongxing.hejiayun.homepage.model.IHomePageModel;
import com.hengtongxing.hejiayun.shop.adapter.GoodsCommentListAdapter;
import com.hengtongxing.hejiayun.shop.adapter.ShopDetailsImgListAdapter;
import com.hengtongxing.hejiayun.shop.model.IShopModel;
import com.hengtongxing.hejiayun.shop.model.ShopModelImpl;
import com.hengtongxing.hejiayun.utils.Dp2Px;
import com.hengtongxing.hejiayun.utils.GlideUtils;
import com.hengtongxing.hejiayun.utils.NumberFormatUtils;
import com.hengtongxing.hejiayun.widget.ViewLoading;
import com.hengtongxing.hejiayun.widget.WrapContentLinearLayoutManager;
import com.stx.xhb.xbanner.XBanner;
import com.stx.xhb.xbanner.transformers.Transformer;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShopGoodsDetailsActivity extends BaseActivity {

    @BindView(R.id.xbanner)
    XBanner xbanner;
    @BindView(R.id.tv_curr_page)
    TextView tvCurrPage;
    @BindView(R.id.tv_total_page)
    TextView tvTotalPage;
    @BindView(R.id.tv_market_price)
    TextView tvMarketPrice;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_more)
    TextView tvMore;
    @BindView(R.id.recyclerView_comment)
    RecyclerView recyclerViewComment;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.nestedScrollView)
    NestedScrollView nestedScrollView;
    @BindView(R.id.view_bg)
    View viewBg;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.ll_back)
    RelativeLayout llBack;
    @BindView(R.id.layout_title)
    LinearLayout layoutTitle;
    @BindView(R.id.ll_cart)
    LinearLayout llCart;
    @BindView(R.id.tv_cart_num)
    TextView tvCartNum;
    @BindView(R.id.ll_cart_num)
    RelativeLayout llCartNum;
    @BindView(R.id.tv_add_cart)
    TextView tvAddCart;
    @BindView(R.id.tv_now_buy)
    TextView tvNowBuy;
    @BindView(R.id.tv_merchant_name)
    TextView tvMerchantName;
    private String goods_id;
    private String query_str;
    private IShopModel shopModel;
    private ShopDetailsBean.DataBean itemListBean;
    private List<String> thumb_list = new ArrayList<>();
    private List<String> desc_images = new ArrayList<>();
    private ShopDetailsImgListAdapter imgListAdapter;
    private IHomePageModel homePageModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_details);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .statusBarDarkFont(true)
                .navigationBarDarkIcon(true)
                .navigationBarColor(R.color.white)
                .statusBarDarkFont(true, 0.2f)
                .init();
        initView();
        requestGoodsShow();
    }

    @Override
    protected void onResume() {
        super.onResume();
        requestShopCartShow();
    }

    public void initView() {
        homePageModel = new HomePageModelImpl();
        shopModel = new ShopModelImpl();
        goods_id = getIntent().getStringExtra("goods_id");
        query_str = getIntent().getStringExtra("query_str");
        if (TextUtils.isEmpty(goods_id)) {
            goods_id = "";
        }
        if (TextUtils.isEmpty(query_str)) {
            query_str = "";
        }

        int height = (int) Dp2Px.dp2Px(126);
        nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (scrollY <= 0) {   //设置标题的背景颜色
                    layoutTitle.setBackgroundColor(Color.argb((int) 0, 255, 255, 255));
                    ivBack.setImageResource(R.mipmap.ic_back_white);
                    viewBg.setVisibility(View.VISIBLE);
                } else if (scrollY > 0 && scrollY <= height) { //滑动距离小于banner图的高度时，设置背景和字体颜色颜色透明度渐变
                    float scale = (float) scrollY / height;
                    float alpha = (255 * scale);
                    layoutTitle.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));
                } else {    //滑动到banner下面设置普通颜色
                    layoutTitle.setBackgroundColor(Color.argb((int) 255, 255, 255, 255));
                    ivBack.setImageResource(R.mipmap.ic_back_black);
                    viewBg.setVisibility(View.GONE);
                }
            }
        });

        xbanner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tvCurrPage.setText((position + 1) + "");
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        imgListAdapter = new ShopDetailsImgListAdapter(desc_images);
        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(mActivity));
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(imgListAdapter);
    }

    public void requestGoodsShow() {
        ViewLoading.showProgress(mActivity, "加载中......");
        shopModel.requestIntegralGoodsShow(goods_id, query_str, new DataCallBack<ShopDetailsBean>() {
            @Override
            public void onSuccessful(ShopDetailsBean bean) {
                if (bean.getData() != null) {
                    if (TextUtils.isEmpty(goods_id)) {
                        goods_id = bean.getData().getId() + "";
                    }
                    itemListBean = bean.getData();
                    if (!TextUtils.isEmpty(itemListBean.getMerchant_name())) {
                        tvMerchantName.setText(itemListBean.getMerchant_name());
                    } else {
                        tvMerchantName.setText("合家云生活");
                    }

                    tvTitle.setText(bean.getData().getName());
                    String money = NumberFormatUtils.formatDouble(Double.parseDouble(bean.getData().getPrice()) / 100);
                    tvMarketPrice.setText(money);

                    thumb_list.clear();
                    for (ShopDetailsBean.DataBean.GoodsPicBean goodsPicBean : itemListBean.getGoods_pic()) {
                        thumb_list.add(goodsPicBean.getPic());
                    }
                    xbanner.setPageTransformer(Transformer.Default);
                    xbanner.setData(R.layout.item_banner_conner_iv2, thumb_list, null);
                    xbanner.loadImage(new XBanner.XBannerAdapter() {
                        @Override
                        public void loadBanner(XBanner banner, Object model, View view, int position) {
                            ImageView view1 = (ImageView) view;
                            GlideUtils.loadImage(mActivity, view1, thumb_list.get(position));
                        }
                    });
                    tvTotalPage.setText("/" + thumb_list.size());
                    if (thumb_list.size() > 0) {
                        tvCurrPage.setText("1");
                    }

                    desc_images.clear();
                    if (itemListBean.getInfo_pic() != null) {
                        for (String pic : itemListBean.getInfo_pic()) {
                            desc_images.add(pic);
                        }
                    }

                    imgListAdapter = new ShopDetailsImgListAdapter(desc_images);
                    recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(mActivity));
                    recyclerView.setNestedScrollingEnabled(false);
                    recyclerView.setAdapter(imgListAdapter);

                    requestCourseCommentIndex();
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    private void requestCourseCommentIndex() {
        homePageModel.requestCourseCommentIndex(1, goods_id, 1, new DataCallBack<CommentListBean>() {
            @Override
            public void onSuccessful(CommentListBean bean) {
                if (bean.getData().getItemList() != null && bean.getData().getItemList().size() > 0) {
                    List<ShopDetailsBean.DataBean.GoodsCommentBean> commentBeanList = new ArrayList<>();
                    if (bean.getData().getItemList() != null) {
                        commentBeanList.addAll(bean.getData().getItemList());
                    }
                    GoodsCommentListAdapter commentListAdapter = new GoodsCommentListAdapter(commentBeanList);
                    recyclerViewComment.setLayoutManager(new WrapContentLinearLayoutManager(mActivity));
                    recyclerViewComment.setNestedScrollingEnabled(false);
                    recyclerViewComment.setAdapter(commentListAdapter);
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    /**
     * 购物车 列表
     */
    public void requestShopCartShow() {
        new ShopModelImpl().requestIntegralShopCartShow(new DataCallBack<ShopCartListBean>() {
            @Override
            public void onSuccessful(ShopCartListBean bean) {
                if (bean.getData() != null && bean.getData().getItemList().size() > 0) {
                    tvCartNum.setText(bean.getData().getItemList().size() + "");
                    llCartNum.setVisibility(View.VISIBLE);
                } else {
                    tvCartNum.setText("");
                    llCartNum.setVisibility(View.GONE);
                }

            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick({R.id.ll_cart, R.id.ll_back, R.id.tv_add_cart, R.id.tv_now_buy, R.id.tv_more})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.ll_cart:
                showActivity(ShopCartActivity.class);
                break;
            case R.id.tv_add_cart:
                if (itemListBean == null) {
                    return;
                }
                ShopCartDialog dialog = ShopCartDialog.newInstance(1, itemListBean);
                dialog.show(getSupportFragmentManager(), "dialog");
                dialog.setOnCallBackListener(type -> {
                    requestShopCartShow();
                });
                break;
            case R.id.tv_now_buy:
                if (itemListBean == null) {
                    return;
                }
                dialog = ShopCartDialog.newInstance(2, itemListBean);
                dialog.show(getSupportFragmentManager(), "dialog");
                dialog.setOnCallBackListener(type -> {

                });
                break;
            case R.id.tv_more:
                Intent intent = new Intent(mActivity, CommentListActivity.class);
                intent.putExtra("goods_id", goods_id);
                startActivity(intent);
                break;
        }
    }
}
